package com.ehotting.edsta.circuitDesign.bean;

import com.ehotting.edsta.circuitDesign.bean.request.RequestCodeExecute;
import com.ehotting.edsta.circuitDesign.domain.EsCircuit;
import com.ehotting.edsta.circuitDesign.domain.EsElement;
import com.ehotting.edsta.circuitDesign.enums.ChannelTypeInputEnum;
import com.ehotting.edsta.circuitDesign.enums.ChannelTypeOutEnum;
import com.ehotting.edsta.circuitDesign.enums.DigitalTypeEnum;
import com.ehotting.edsta.circuitDesign.gen.element.helper.NumberHelper;
import org.apache.commons.lang3.SerializationUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 * 电路元器件
 */
public class CircuitElementBean implements Cloneable, Serializable {

    /***
     * 电路
     */
    private EsCircuit circuit;

    /***
     * 元器件
     */
    private List<Map<String, EsElement>> elementList = new ArrayList<>();

    private Double power = 0.0;
    private Double area = 0.0;
    private Double price = 0.0;

    /**
     * 电路属性通道数
     */
    private Integer channelNum = 0;
    /**
     * 通道类型
     */
    private ChannelTypeInputEnum channelType = ChannelTypeInputEnum.OTHER;

    /**
     * 个数
     */
    private Integer count =1 ;

    private String bus = null;

    private List<RequestCodeExecute.InputChannel> channelList = new ArrayList();


    public CircuitElementBean() {

    }

    public CircuitElementBean(Long id, Integer sample,Integer channelNum,Integer numberOfDigits) {
        this.circuit = new EsCircuit();
        circuit.setId(id);
        circuit.getProp().put(ProCodeName.Sample,sample);
        circuit.getProp().put(ProCodeName.Channel_Num,channelNum);
        circuit.getProp().put(ProCodeName.Number_Of_Digits,numberOfDigits);
        this.channelNum = channelNum;
    }

    public CircuitElementBean clone()  {
        return  SerializationUtils.clone(this);
    }
    public CircuitElementBean(EsCircuit circuit) {
        this.circuit = circuit;
    }
    public CircuitElementBean(EsCircuit circuit, Map<String, EsElement> element) {
        this.circuit = circuit;
        if(element!=null){
            this.elementList.add(element) ;
        }
    }

    public Long getId(){
        return circuit.getId();
    }


    public void add(){
        this.count++;
    }

    public EsCircuit getCircuit() {
        return circuit;
    }

    public void setCircuit(EsCircuit circuit) {
        this.circuit = circuit;
    }

    public List<Map<String, EsElement>> getElementList() {
        return elementList;
    }

    public void setElement(List<Map<String, EsElement>> elementList) {
        this.elementList = elementList;
    }

    public Double getPower() {
        return power;
    }

    public void setPower(Double power) {
        this.power = power;
    }

    public Double getArea() {
        return area;
    }

    public void setArea(Double area) {
        this.area = area;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getBus() {
        return bus;
    }

    public void setBus(String bus) {
        this.bus = bus;
    }

    public Integer getChannelNum() {
        Integer channelNum = 0;
        if(circuit.getProp().containsKey(ProCodeName.Channel_Num)){
            channelNum =  NumberHelper.parseInteger(circuit.getProp().get(ProCodeName.Channel_Num).toString());
        }
        return channelNum;
    }


    public ChannelTypeInputEnum getChannelTypeInput() {
        if(circuit.getProp().containsKey(ProCodeName.Channel_Type)){
            Integer channelTypeInt = NumberHelper.parseInteger(circuit.getProp().get(ProCodeName.Channel_Type).toString());
            ChannelTypeInputEnum channelType = ChannelTypeInputEnum.getByValue(channelTypeInt);
            return channelType;
        }
        return ChannelTypeInputEnum.OTHER;
    }


    public ChannelTypeOutEnum getChannelTypeOut() {
        if(circuit.getProp().containsKey(ProCodeName.Channel_Type_Out)){
            Integer channelTypeInt = NumberHelper.parseInteger(circuit.getProp().get(ProCodeName.Channel_Type_Out).toString());
            ChannelTypeOutEnum channelType = ChannelTypeOutEnum.getByValue(channelTypeInt);
            return channelType;
        }
        return ChannelTypeOutEnum.OTHER;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Integer getCount() {
        return count;
    }

    public List<RequestCodeExecute.InputChannel> getChannelList() {
        return channelList;
    }

    public void setChannelList(List<RequestCodeExecute.InputChannel> channelList) {
        this.channelList = channelList;
    }

    public int getSample(){
        Integer sample = 0;
        if(circuit.getProp().containsKey(ProCodeName.Sample)){
            sample =  NumberHelper.parseInteger(circuit.getProp().get(ProCodeName.Sample).toString());
        }
        return sample;
    }

    public int getNumberOfDigits(){
        Integer numberOfDigits = 0;
        if(circuit.getProp().containsKey(ProCodeName.Number_Of_Digits)){
            numberOfDigits =  NumberHelper.parseInteger(circuit.getProp().get(ProCodeName.Number_Of_Digits).toString());
        }
        return numberOfDigits;
    }

    public float getFMax(){
        float fMax = 0.00f;
        if(circuit.getProp().containsKey(ProCodeName.F_Max)){
            fMax =  Float.valueOf(circuit.getProp().get(ProCodeName.F_Max).toString());
        }
        return fMax;
    }

    public float getFMin(){
        float fMin = 0.00f;
        if(circuit.getProp().containsKey(ProCodeName.F_Min)){
            fMin =  Float.valueOf(circuit.getProp().get(ProCodeName.F_Min).toString());
        }
        return fMin;
    }

    public double getInputMaxRange(){
        double inpuMaxRange = 0.00;
        if(circuit.getProp().containsKey(ProCodeName.Input_Max_Range)){
            inpuMaxRange =  Double.valueOf(circuit.getProp().get(ProCodeName.Input_Max_Range).toString());
        }
        return inpuMaxRange;
    }

    public double getInputMinRange(){
        double inpuMinRange = 0.00;
        if(circuit.getProp().containsKey(ProCodeName.Input_Min_Range)){
            inpuMinRange =  Double.valueOf(circuit.getProp().get(ProCodeName.Input_Min_Range).toString());
        }
        return inpuMinRange;
    }

    public String getInputVoltageRange(){
        String inputVoltageRange = "";
        if(circuit.getProp().containsKey(ProCodeName.Input_Voltage_Range)){
            inputVoltageRange =  circuit.getProp().get(ProCodeName.Input_Voltage_Range).toString();
        }
        return inputVoltageRange;
    }

    public DigitalTypeEnum getInType(){
        if(circuit.getProp().containsKey(ProCodeName.In_Type)){
            String code = circuit.getProp().get(ProCodeName.In_Type).toString();
            DigitalTypeEnum digitalType = DigitalTypeEnum.getByCode(code);
            return digitalType;
        }
        return DigitalTypeEnum.Order;
    }

    public Integer getSamplingLength(){
        Integer samplingLength = 0;
        if(circuit.getProp().containsKey(ProCodeName.Sampling_Length)){
            samplingLength = Integer.valueOf(circuit.getProp().get(ProCodeName.Sampling_Length).toString());
        }
        return samplingLength;
    }

    public String getSenseModel(){
        if(circuit.getProp().containsKey(ProCodeName.Sense_Model)){
            String senseModel = circuit.getProp().get(ProCodeName.Sense_Model).toString();
            return senseModel;
        }
        return null;
    }

}
